import 'package:custom_widgets/cache_util.dart';
import 'package:custom_widgets/item/item_normal_widget.dart';
import 'package:custom_widgets/page/theme_demo_page.dart';
import 'package:flutter/material.dart';
import 'package:mi_mang_flutter/config/configs.dart';
import 'package:mi_mang_flutter/provider/locale_provider.dart';
import 'package:mi_mang_flutter/res/dimens.dart';
import 'package:mi_mang_flutter/res/styles.dart';
import 'package:mi_mang_flutter/routers/fluro_navigator.dart';
import 'package:mi_mang_flutter/utils/dialog_util.dart';
import 'package:mi_mang_flutter/utils/locale_util.dart';
import 'package:mi_mang_flutter/utils/toast_util.dart';
import 'package:provider/provider.dart';

import '../setting_router.dart';

class CommonSettingPage extends StatefulWidget {
  CommonSettingPage({Key? key}) : super(key: key);

  @override
  _CommonSettingPageState createState() => _CommonSettingPageState();
}

class _CommonSettingPageState extends State<CommonSettingPage> {
  /// 缓存大小
  String _cacheSize = "0B";

  @override
  void initState() {
    super.initState();

    /// 获取缓存大小
    loadCache().then((value) {
      setState(() {
        _cacheSize = value;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(i18n(context).commonSetting),
        centerTitle: true,
      ),
      body: _buildBody(),
    );
  }

  Widget _buildBody() {
    var provider = Provider.of<LocaleProvider>(context);
    return ListView(
      padding: EdgeInsets.symmetric(vertical: Dimens.gapDp10),
      children: <Widget>[
        ...ItemNormalWidget.divideTiles(childs: <Widget>[
          ItemNormalWidget.item(
            title: i18n(context).theme,
            onTap: () {
              NavigatorUtils.push(context, SettingRouter.commonSettingTheme);
            },
          ),
          ItemNormalWidget.item(
            title: i18n(context).language,
            // subtitle: i18n(context).autoBySystem,
            subtitle: LocaleProvider.localeName(provider.localeIndex, context),
            onTap: () {
              NavigatorUtils.push(context, SettingRouter.commonSettingLanguage);
            },
          ),
        ]),
        Gaps.vGap10,
        ...ItemNormalWidget.divideTiles(childs: <Widget>[
          ItemNormalWidget.item(
            title: i18n(context).font,
            onTap: () {
              NavigatorUtils.push(context, SettingRouter.commonSettingFont);
            },
          ),
          ItemNormalWidget.item(
            title: i18n(context).fontSize,
            onTap: () {
              NavigatorUtils.push(context, SettingRouter.commonSettingFontSize);
            },
          ),
        ]),
        Gaps.vGap10,
        ItemNormalWidget.item(
          title: i18n(context).clearCache,
          subtitle: _cacheSize,
          onTap: () {
            DialogUtil.showDialogMsg(
              context,
              msg: "确定清空缓存?",
              onPressed: () {
                clearCache().then((value) {
                  Toast.showInfo(value
                      ? i18n(context).clearCacheSuccess
                      : i18n(context).clearCacheFailure);
                });
              },
            );
          },
        ),
        Gaps.vGap10,
        ..._buildDemoWidget(),
      ],
    );
  }

  List<Widget> _buildDemoWidget() {
    if (!Configs.DEBUG) return [SizedBox.shrink()];
    return ItemNormalWidget.divideTiles(childs: <Widget>[
      ItemNormalWidget.item(
        title: "测试主题Demo",
        onTap: () {
          // 测试主题Demo
          Navigator.push<dynamic>(
            context,
            MaterialPageRoute<dynamic>(
              builder: (BuildContext context) => ThemeDemoPage(),
            ),
          );
        },
      ),
    ]).toList();
  }
}
